package com.kmzx.one.project.config.wx;

import com.github.binarywang.wxpay.bean.order.WxPayMpOrderResult;
import com.github.binarywang.wxpay.bean.request.WxPayUnifiedOrderRequest;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.service.WxPayService;
import com.kmzx.one.project.config.data.WXDataConfig;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.text.DecimalFormat;

@Component
public class WxPayUtils {

    @Autowired
    private WxPayService wxService;

    @Autowired
    private WXDataConfig wxDataConfig;

    public WxPayMpOrderResult payOrder(String nonce, String openId, BigDecimal payMoney, String orderNo, String productName, String orderId) {
        //用 getLocalHost() 方法创建的InetAddress的对象

        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        DecimalFormat df = new DecimalFormat("#");
        String money = df.format(payMoney.multiply(new BigDecimal(100)));
        request.setOpenid(openId);
        request.setOutTradeNo(orderNo);
        request.setBody(productName);
        request.setTotalFee(Integer.parseInt(money));
        request.setNonceStr(nonce);
        request.setTradeType("JSAPI");
        request.setNotifyUrl(wxDataConfig.getCallbackUrl() + "/" + orderId);

        try {
            InetAddress address = InetAddress.getLocalHost();
            request.setSpbillCreateIp(address.getHostAddress());
            WxPayMpOrderResult order = (WxPayMpOrderResult) wxService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }


    public WXTwo payOrderIP(String nonce, String openId, BigDecimal payMoney, String orderNo, String productName, String orderId, String ip) {
        //用 getLocalHost() 方法创建的InetAddress的对象

        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        DecimalFormat df = new DecimalFormat("#");
        String money = df.format(payMoney.multiply(new BigDecimal(100)));
        request.setOpenid(openId);
        request.setOutTradeNo(orderNo);
        request.setBody(productName);
        request.setTotalFee(Integer.parseInt(money));
        request.setNonceStr(nonce);
        request.setTradeType("JSAPI");
        request.setNotifyUrl(wxDataConfig.getCallbackUrl() + "/" + orderId);

        request.setSpbillCreateIp(ip);
        try {
            WXTwo two = new WXTwo();
            WxPayMpOrderResult result = (WxPayMpOrderResult) wxService.createOrder(request);
            two.setResult(result);
            two.setReq(request);
            return two;
        } catch (WxPayException e) {
            e.printStackTrace();
        }
        return null;
    }


    public WxPayMpOrderResult payUserRankBuy(String nonce, String openId, BigDecimal payMoney, String orderNo, String productName, String orderId) {
        //用 getLocalHost() 方法创建的InetAddress的对象

        WxPayUnifiedOrderRequest request = new WxPayUnifiedOrderRequest();
        DecimalFormat df = new DecimalFormat("#");
        String money = df.format(payMoney.multiply(new BigDecimal(100)));
        request.setOpenid(openId);
        request.setOutTradeNo(orderNo);
        request.setBody(productName);
        request.setTotalFee(Integer.parseInt(money));
        request.setNonceStr(nonce);
        request.setTradeType("JSAPI");
        request.setNotifyUrl(wxDataConfig.getRankBuy() + "/" + orderId);

        try {
            InetAddress address = InetAddress.getLocalHost();
            request.setSpbillCreateIp(address.getHostAddress());
            WxPayMpOrderResult order = (WxPayMpOrderResult) wxService.createOrder(request);
            return order;
        } catch (WxPayException e) {
            e.printStackTrace();
        } catch (UnknownHostException e) {
            e.printStackTrace();
        }
        return null;
    }


}
